package cn.stylefeng.guns.modular.transport.controller;

import java.util.List;

import cn.stylefeng.guns.modular.basic.model.result.BasicCarrierResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.model.params.BasicCarrierParam;
import cn.stylefeng.guns.modular.basic.service.BasicCarrierService;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.transport.entity.TransportMsg;
import cn.stylefeng.guns.modular.transport.model.params.TDispatchParam;
import cn.stylefeng.guns.modular.transport.model.params.TransportMsgParam;
import cn.stylefeng.guns.modular.transport.model.result.TransportMsgResult;
import cn.stylefeng.guns.modular.transport.service.TransportMsgService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;


/**
 * 运输信息表控制器
 */
@Controller
@RequestMapping("/transportMsg")
public class TransportMsgController extends BaseController {

    private String PREFIX = "/transport/transportMsg";

    @Autowired
    private TransportMsgService transportMsgService;

    @Autowired
    private UserService userService;
    
    @Autowired
    private BasicCustomerService basicCustomerService;

    @Autowired
    private BasicOperateService basicOperateService;

    @Autowired
    private BasicCarrierService basicCarrierService;

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/transportMsg.html";
    }

    /**
     * 跳转到到仓签到
     * 查询页面
     */
    @RequestMapping("todriverSignin")
    public String todriverSignin(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/driverSignin.html";
    }

    /**
     * 运输信息
     */
    @RequestMapping("/transportMsg")
    @ResponseBody
    public ResponseData transportMsg(TDispatchParam tDispatchParam) {
    	User user = userService.getcurrentUser();
        TransportMsgParam transportMsgParam = new TransportMsgParam();
        transportMsgParam.setDeliverId(userService.getDeliverId());
        transportMsgParam.setDeptId(userService.getDeptId());
        transportMsgParam.setDispatchNumber(tDispatchParam.getDispatchNumber());
        List<TransportMsgResult> list = transportMsgService.findListBySpec(transportMsgParam,user.getOperateId());
        return ResponseData.success(list);
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/transportMsg_add.html";
    }

    /**
     * 编辑页面
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/transportMsg_edit.html";
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(TransportMsgParam transportMsgParam) {
        this.transportMsgService.add(transportMsgParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(TransportMsgParam transportMsgParam) {
        this.transportMsgService.update(transportMsgParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(TransportMsgParam transportMsgParam) {
        this.transportMsgService.delete(transportMsgParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(TransportMsgParam transportMsgParam) {
        TransportMsg detail = this.transportMsgService.getById(transportMsgParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(TransportMsgParam transportMsgParam,String condition ) {
        return this.transportMsgService.findPageBySpec(transportMsgParam,condition);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list_driverSignin")
    public LayuiPageInfo list_driverSignin(TransportMsgParam transportMsgParam,String condition ,String startTime,String endTime) {
    	User user = userService.getcurrentUser();
    	transportMsgParam.setStatus("到仓签到");
    	if (ToolUtil.isNotEmpty(startTime) && ToolUtil.isNotEmpty(endTime)) {
    		transportMsgParam.setTimes("'"+startTime+"' and '"+endTime+"'");
		} else if (startTime == null && endTime == null){
			startTime = DateUtil.getDate();
			endTime = DateUtil.getDate();
    		transportMsgParam.setTimes("'"+startTime+"' and '"+endTime+"'");
		}

        //查看当前账户是否是承运商，若是承运商只能查看自己的数据
        BasicCarrierResult basicCarrier = basicCarrierService.findBySpec(BasicCarrierParam.builder()
                .deptId(user.getDeptId())
                .carrierAccount(user.getAccount())
                .build(), user.getOperateId());
        if(basicCarrier != null){
            transportMsgParam.setCarrierId(basicCarrier.getId().longValue());
        }

    	transportMsgParam.setDeptId(user.getDeptId());
        return this.transportMsgService.finddriverSigninPageBySpec(transportMsgParam,condition,user.getOperateId());
    }

}


